package com.b2b.util;

import com.b2b.exception.CustomException;

import com.b2b.model.CompanyUserRoleDto;
import com.b2b.service.CompanyUserRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CompanyUserRoleUtil {
    private final CompanyUserRoleService companyUserRoleService;
    @Autowired
    public CompanyUserRoleUtil(CompanyUserRoleService companyUserRoleService) {
        this.companyUserRoleService = companyUserRoleService;
    }


    /**
     * 获取当前登录用户企业Id
     *
     * @param
     * @return com.b2b.model.UserDto
     * @author wliduo[i@dolyw.com]
     * @date 2019/3/15 11:48
     */
    public CompanyUserRoleDto getCompanyId(Integer Id) {
        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
        companyUserRoleDto.setUserId(Id);
        companyUserRoleDto = companyUserRoleService.selectOne(companyUserRoleDto);
        if (companyUserRoleDto == null) {
            throw new CustomException("该帐号不存在(The account does not exist.)");
        }
        return companyUserRoleDto;
    }

    public List<Integer> getUserList(Integer companyId){
        List<Integer> userList = new ArrayList<>();
        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
        companyUserRoleDto.setCompanyId(companyId);
        List<CompanyUserRoleDto> companyUserRoleDtoList = companyUserRoleService.select(companyUserRoleDto);
        for (CompanyUserRoleDto value : companyUserRoleDtoList){
            Integer userId = value.getUserId();
            userList.add(userId);
        }
        return userList;
    }
    public Integer deleteCompanyUserRoleDto(Integer userId){
        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
        companyUserRoleDto.setUserId(userId);
        Integer count = companyUserRoleService.deleteByPrimaryKey(companyUserRoleDto);
        return count;
    }
    public Integer addCompanyUserRoleDto(CompanyUserRoleDto companyUserRoleDto){
        Integer count = companyUserRoleService.insert(companyUserRoleDto);
        return count;
    }

    public Integer addNewUser(Integer userId, Integer companyId){
        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
        companyUserRoleDto.setCompanyId(companyId);
        companyUserRoleDto.setUserId(userId);
        companyUserRoleDto.setRoleId(0);
        Integer count = companyUserRoleService.insert(companyUserRoleDto);
        return count;
    }
}
